package main

import (
	"github.com/gookit/color"
	"io"
	"log"
	"os"
)

var flags = log.LstdFlags | log.Lshortfile | log.Lmicroseconds | log.Lmsgprefix

// #颜色支持 blue蓝，green绿色，red红色，yellow黄色，cyan青色
// 'DEBUG': 'cyan',
// 'INFO': 'green',
// 'WARNING': 'yellow',
// 'ERROR': 'red',
// 'CRITICAL': 'bold_red',

type ColorWriter struct {
	color.Color
}

func (c ColorWriter) Write(p []byte) (n int, err error) {
	return os.Stdout.Write([]byte(c.Color.Renderln(string(p))))
}
func NewColorWriter(color color.Color) *ColorWriter {
	return &ColorWriter{color}
}

var Info = log.New(NewColorWriter(color.Green), "[INFO ] ", flags)
var Error = log.New(NewColorWriter(color.Red.Light()), "[ERROR] ", flags)
var Warn = log.New(NewColorWriter(color.Yellow), "[WARN ] ", flags)
var Debug = log.New(NewColorWriter(color.Cyan), "[DEBUG] ", flags)

func init() {
	log.Default()
	log.SetFlags(0) // 关闭日志时间戳
	log.SetPrefix("[INFO] ")
	log.SetOutput(os.Stdout)
	log.SetFlags(flags)
}

// /////////////////////////////////////
type nullWriter struct {
}

func (c nullWriter) Write(p []byte) (n int, err error) {
	return 0, nil
}
func NullWriter() io.Writer {
	return &nullWriter{}
}
